package com.example.administrator.base.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.base.activity.ChangePassword;
import com.example.administrator.base.activity.Login;
import com.example.administrator.base.activity.OrderList;
import com.example.administrator.base.activity.PointMessage;
import com.example.administrator.base.activity.ResetphoneActivity;
import com.example.administrator.base.activity.ScanActivity;
import com.example.administrator.base.application.App;
import com.example.administrator.base.bean.ReLogout;
import com.example.administrator.base.R;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.SharedPreferencesUtils;
import java.io.IOException;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class MineFragment extends Fragment {


    @InjectView(R.id.tv1)
    TextView tv1;
    @InjectView(R.id.tv2)
    TextView tv2;
    SweetAlertDialog sweetAlertDialog ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("337845818", "Start_mine");
        sweetAlertDialog = new SweetAlertDialog(getActivity(), 5);
        sweetAlertDialog.setCanceledOnTouchOutside(false);
        sweetAlertDialog.setTitleText("加载中");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        ButterKnife.inject(this, view);
        String type=String.valueOf(SharedPreferencesUtils.getParam(getActivity(),"administratorType",""));
        String name=String.valueOf(SharedPreferencesUtils.getParam(getActivity(),"realName",""));
        tv2.setText(name);
        switch (type){
            case "1":
                tv1.setText("运维人员");
                break;
            case "2":
                tv1.setText("调度人员");
                break;
            case "3":
                tv1.setText("维修人员");
                break;
            case "0":
                tv1.setText("督导人员");
                break;
            case "4":
                tv1.setText("兼职人员");
                break;
            case "5":
                tv1.setText("高阶运维人员");
                break;
        }
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick({R.id.rl_ResetPhone, R.id.rl_ResetPassword, R.id.rl_MyOrder, R.id.rl_StationMessage, R.id.rl_LockCar, R.id.rl_SignIn, R.id.rl_SignOut, R.id.btn_logout})
    public void onViewClicked(View view) {
        String address=String.valueOf(SharedPreferencesUtils.getParam(getActivity(), "address", ""));
        String lat = String.valueOf(SharedPreferencesUtils.getParam(getActivity(), "latitude", ""));
        String lon = String.valueOf(SharedPreferencesUtils.getParam(getActivity(), "longtitude", ""));
        switch (view.getId()) {
            case R.id.rl_ResetPhone:
                SweetAlertDialog sweetAlertDialognew = new SweetAlertDialog(getActivity(), SweetAlertDialog.WARNING_TYPE);
                sweetAlertDialognew.setCanceledOnTouchOutside(true);
                sweetAlertDialognew
                        .setTitleText("是否更换过手机号")
                        .setContentText("如果您的原手机已经弃用，无法收到验证码，请选择“是”")
                        .setConfirmText("是")
                        .setCancelText("否")
                        .showCancelButton(true)
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sDialog) {
                                Intent intent=new Intent(getActivity(),ResetphoneActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                intent.putExtra("havephone","0");
                                startActivity(intent);
                                sDialog.dismiss();

                            }
                        })
                        .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                Intent intent=new Intent(getActivity(),ResetphoneActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                intent.putExtra("havephone","1");
                                startActivity(intent);
                                sweetAlertDialog.dismiss();
                            }
                        })

                        .show();
                break;
            case R.id.rl_ResetPassword:
                Intent intent = new Intent(getActivity(), ChangePassword.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
                break;
            case R.id.rl_MyOrder:
                Intent intentorder = new Intent(getActivity(), OrderList.class);
                intentorder.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intentorder);
                break;
            case R.id.rl_StationMessage:
                Intent intentpoint = new Intent(getActivity(), PointMessage.class);
                intentpoint.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intentpoint);
                break;
            case R.id.rl_LockCar:
                Intent intentscan = new Intent(getActivity(), ScanActivity.class);
                intentscan.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                intentscan.putExtra("type","lockCar");
                startActivity(intentscan);
                break;
            case R.id.rl_SignIn:
                sweetAlertDialog.show();
                signin("0",address,lat,lon);
                break;
            case R.id.rl_SignOut:
                sweetAlertDialog.show();
                signin("1",address,lat,lon);
                break;
            case R.id.btn_logout:
                sweetAlertDialog.show();
                logout();
                break;
        }
    }
    /*退出登录*/
    public void logout() {
        Log.i("3378", String.valueOf(SharedPreferencesUtils.getParam(getActivity(), "adminId", "")));
        final OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("action", "LOGOUT")
                .add("adminId", String.valueOf(SharedPreferencesUtils.getParam(getActivity(), "adminId", "")))
                .add("type", "1")
                .build();

        final Request request = new Request.Builder()
                .url(App.getUrl())
                .post(formBody)
                .build();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response ;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        String string = response.body().string();
                        Log.i("456921", string);
                        ReLogout mse = GsonTools.changeGsonToBean(string, ReLogout.class);
                        if (mse!=null) {
                            if (mse.getCode() == 1) {
                                SharedPreferencesUtils.setParam(getActivity(), "adminId", "");
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        sweetAlertDialog.dismissWithAnimation();
                                        Toast.makeText(getActivity(), "退出登录成功", Toast.LENGTH_LONG).show();
                                        startActivity(new Intent(getActivity(), Login.class));
                                        getActivity().finish();
                                    }
                                });

                            }
                        }else {
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    sweetAlertDialog.dismissWithAnimation();
                                    Toast.makeText(getActivity(), "数据异常请重试", Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                    } else {
                        throw new IOException("Unexpected code " + response);
                    }
                } catch (IOException e) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            sweetAlertDialog.dismissWithAnimation();
                        }
                    });
                    e.printStackTrace();
                }
            }
        }).start();


    }
    /*签到签退*/
    public void signin(String flag, String address, String lat, String lon) {
        Log.i("3378", String.valueOf(SharedPreferencesUtils.getParam(getActivity(), "adminId", "")));
        Log.i("3378", flag + address + lat + lon);
        final OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("action", "ATTENDANCE")
                .add("clockFlag", flag)
                .add("address", address)
                .add("latitude", lat)
                .add("longitude", lon)
                .add("adminId", String.valueOf(SharedPreferencesUtils.getParam(getActivity(), "adminId", "")))
                .build();

        final Request request = new Request.Builder()
                .url(App.getUrl())
                .post(formBody)
                .build();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response ;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        String string = response.body().string();
                        final ReLogout mse = GsonTools.changeGsonToBean(string, ReLogout.class);
                        if (mse!=null) {
                            if (mse.getCode() == 1) {
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getActivity(), "" + mse.getMessage(), Toast.LENGTH_LONG).show();
                                        sweetAlertDialog.dismiss();
                                    }
                                });
                            }
                        }else {
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getActivity(), "数据异常请重试" , Toast.LENGTH_LONG).show();
                                    sweetAlertDialog.dismiss();
                                }
                            });
                        }

                    } else {
                        throw new IOException("Unexpected code " + response);
                    }
                } catch (IOException e) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            sweetAlertDialog.dismiss();
                        }
                    });
                    e.printStackTrace();
                }
            }
        }).start();


    }
}
